package com.lookpoint.util;

import java.io.Serializable;

/**
 * 统一Controller中RESTFul风格接口返回的结果
 */
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer code;
    private String msg;
    private Object data;

    public Result() {
    }

    // 返回状态码 + data
    private Result(ResultCode status, T data) {
        this.code = status.code;
        this.msg = status.msg;
        this.data = data;
    }

    // 返回状态码
    private Result(ResultCode status) {
        this.code = status.code;
        this.msg = status.msg;
    }

    // 返回状态码 + 自定义信息
    private Result(ResultCode status, String msg) {
        this.code = status.code;
        this.msg = msg;
    }
    // 成功 + 不带数据
    public static <T> Result<T> ok() {
        return new Result<>(ResultCode.OK);
    }
    // 成功 + 带数据
    public static <T> Result<T> ok(T data) {
        return new Result<>(ResultCode.OK, data);
    }
    // 不成功 + 不带数据
    public static <T> Result<T> error(ResultCode status) {
        return new Result<>(status);
    }
    // 成功 + 带数据
    public static <T> Result<T> error(ResultCode status, String msg) {
        return new Result<>(status, msg);
    }
}
